package com.atguigu.seckill.controller;

import com.atguigu.common.utils.R;
import com.atguigu.seckill.service.SeckillService;
import com.atguigu.seckill.to.SeckillSkuRedisTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.List;

/**
 * description: SeckillController
 * date: 2023/1/6 18:41
 * author: MR.孙
 */
@Slf4j
@Controller
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    /**
     * 返回当前时间可以参与的秒杀商品信息
     * @return
     */
    @ResponseBody
    @GetMapping(value="/currentSeckillSkus")
    public R getCurrentSeckillSkus(){
        log.info("currentSeckillSkus正在执行");
        List<SeckillSkuRedisTo> vos =  seckillService.getCurrentSeckillSkus();
        return R.ok().setData(vos);
    }

    /**
     * 根据skuId获取当前商品的秒杀信息
     * @param skuId
     * @return
     */
    @GetMapping(value="/sku/seckill/{skuId}")
    @ResponseBody
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
        SeckillSkuRedisTo to= seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(to);
    }

    /**
     * 秒杀请求
     * @return
     */
    @GetMapping("/kill")
    public String kill(@RequestParam("killId") String killId,
                  @RequestParam("key") String key,
                  @RequestParam("num") Integer num,
                       Model model) throws InterruptedException {
        //秒杀登录检查
        String orderSn= seckillService.kill(killId, key, num);
        model.addAttribute("orderSn",orderSn);

        return "success";
    }


}
